package com.fetnet.smsalarm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.Toast;

public class TextMessageReceiver extends BroadcastReceiver{

	 static boolean vibrate1_on = true;
	 static String KeyWord1 = null;
	 int volume1 = 0;
	    
	 static boolean vibrate2_on = true;
	 static String KeyWord2 = null;
	 long[] vibratepattern = {100, 400, 500, 400};
	 int volume2 = 0;
	 
	public void onReceive(Context context, Intent intent)
	{
		Bundle bundle=intent.getExtras();
		
		Object[] messages=(Object[])bundle.get("pdus");
		SmsMessage[] sms=new SmsMessage[messages.length];
		
		for(int n=0;n<messages.length;n++){
			sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
		}
		try {
			File myFile = new File("/sdcard/smsalarm1.cfg");
			FileInputStream fIn = new FileInputStream(myFile);
			BufferedReader myReader = new BufferedReader(
					new InputStreamReader(fIn));
			String aDataRow = "";
			String aBuffer = "";
			while ((aDataRow = myReader.readLine()) != null) {
				aBuffer += aDataRow + "\n";
			}
			String[] txtData = aBuffer.split(",");
			KeyWord1 = txtData[0];
			volume1 = Integer.valueOf(txtData[1]);

			
			if(txtData[2].contains("true")){
				vibrate1_on = true;
			} else {
				vibrate1_on = false;
			}
			myReader.close();
		} catch (Exception e) {
			
		}
			try {
				File myFile2 = new File("/sdcard/smsalarm2.cfg");
				FileInputStream fIn2 = new FileInputStream(myFile2);
				BufferedReader myReader2 = new BufferedReader(
						new InputStreamReader(fIn2));
				String aDataRow2 = "";
				String aBuffer2 = "";
				while ((aDataRow2 = myReader2.readLine()) != null) {
					aBuffer2 += aDataRow2 + "\n";
				}
				String[] txtData2 = aBuffer2.split(",");
				KeyWord2 = txtData2[0];
				volume2 = Integer.valueOf(txtData2[1]);

				
				if(txtData2[2].contains("true")){
					vibrate2_on = true;
				} else {
					vibrate2_on = false;
				}
				myReader2.close();
			} catch (Exception e) {
				
			}
		for(SmsMessage msg:sms){
			String textmsg = msg.getMessageBody();
			
			if(KeyWord1 != null) {
			if(textmsg.contains(KeyWord1)) {
				if(vibrate1_on) {	  
					Uri alarmSound = Uri.parse("android.resource://com.example.smsalarm/" + R.raw.alarm);
					MainActivity.notification = new Notification.Builder(context)
			         .setContentTitle("SMS Alarm "+KeyWord1)
			         .setContentText("")
			         .setSmallIcon(R.drawable.ic_launcher)
			         .setSound(alarmSound)
			         .setVibrate(vibratepattern)
			         .build();
				} else {

				Uri alarmSound = Uri.parse("android.resource://com.example.smsalarm/" + R.raw.alarm);
				MainActivity.notification = new Notification.Builder(context)
		         .setContentTitle("SMS Alarm "+KeyWord1)
		         .setContentText("")
		         .setSmallIcon(R.drawable.ic_launcher)
		         .setSound(alarmSound)
		         .build();
				}
				MainActivity.notification.flags |= Notification.FLAG_AUTO_CANCEL;

				MainActivity.notificationManager.notify(0, MainActivity.notification);

				
			}
			
			}
			if(KeyWord2 != null) {
				if(textmsg.contains(KeyWord2)) {
					if(vibrate2_on) {	  
						Uri alarmSound = Uri.parse("android.resource://com.example.smsalarm/" + R.raw.alarm);
						MainActivity.notification = new Notification.Builder(context)
				         .setContentTitle("SMS Alarm "+KeyWord2)
				         .setContentText("")
				         .setSmallIcon(R.drawable.ic_launcher)
				         .setSound(alarmSound)
				         .setVibrate(vibratepattern)
				         .build();
					} else {

					Uri alarmSound = Uri.parse("android.resource://com.example.smsalarm/" + R.raw.alarm);
					MainActivity.notification = new Notification.Builder(context)
			         .setContentTitle("SMS Alarm "+KeyWord2)
			         .setContentText("")
			         .setSmallIcon(R.drawable.ic_launcher)
			         .setSound(alarmSound)
			         .build();
					}
					MainActivity.notification.flags |= Notification.FLAG_AUTO_CANCEL;

					MainActivity.notificationManager.notify(0, MainActivity.notification);

					
				}
				
				}
		}
	}
}